package com.example.composestudy.view.activity

import com.example.base.BaseActivity
import com.example.base.commonView.CommonCard
import com.example.base.commonView.CommonText
import com.example.base.commonView.CommonTheme
import com.example.base.commonView.TitleBar
import android.content.Context
import android.os.Bundle
import androidx.compose.foundation.clickable
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumnFor
import androidx.compose.material.Icon
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ContextAmbient
import androidx.compose.ui.platform.setContent
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.ui.tooling.preview.Preview
import com.example.base.ui.*
import com.example.base.utils.*
import com.example.composestudy.R
import getHomeListData

class MainActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            showView(getHomeListData())
        }
    }
}

@Composable
fun showView(items: List<String>, isDark: Boolean = isSystemInDarkTheme()) {
    //获取上下文
    val context = ContextAmbient.current

    CommonTheme {
        //竖向布局
        Column(modifier = Modifier.fillMaxSize()) {
            //添加titleBar
            TitleBar(isShowBack = false) {}
            /**
             *  类似于使用了缓存的ListView
             *  A vertically scrolling list that only composes and lays out the currently visible items.
             */
            LazyColumnFor(items = items, modifier = Modifier.fillMaxSize()) { item ->
                //CardView
                CommonCard(backgroundColor = if (isDark) darkGray else gray)
                {
                    //横向布局
                    Row(
                        //设置竖向居中
                        verticalAlignment = Alignment.CenterVertically,
                        //设置中间填充
                        horizontalArrangement = Arrangement.SpaceBetween,
                        //设置尺寸属性，并设置点击事件
                        modifier = Modifier.fillParentMaxWidth().clickable(
                            indication = null,
                            onClick = {
                                switchGoTo(context = context, item = item)
                            }).padding(16.dp)
                    ) {
                        CommonText(
                            color = if (isDark) white else black,
                            text = item,
                            fontSize = 16.sp,
                        )

                        Icon(
                            asset = imageResource(R.mipmap.base_icon_arrow),
                            modifier = Modifier.size(15.dp)
                        )

                    }
                }
            }
        }
    }
}

/**
 * 路由
 */
fun switchGoTo(context: Context, item: String) {
    when (item) {
        "Text" -> goTo(context, TextShowActivity)
        "Button" -> goTo(context, ButtonShowActivity)
        "状态管理" -> goTo(context, StateActivity)
        "ScrollView" -> goTo(context, ScrollViewActivity)
        "CustomView" -> goTo(context, CustomViewActivity)
        "NavigationView" -> goTo(context, NavigationViewActivity)
        "LiveData" -> goTo(context, LiveDataActivity)
        "Image" -> goTo(context, ImageActivity)
    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    showView(getHomeListData(), isSystemInDarkTheme())
}